package de.fhluebeck.oop.gps.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.fhluebeck.oop.gps.common.Constants;
import de.fhluebeck.oop.gps.common.DependencyManager;
import de.fhluebeck.oop.gps.common.Singleton;
import de.fhluebeck.oop.gps.dao.IDAO;
import de.fhluebeck.oop.gps.entity.Category;
import de.fhluebeck.oop.gps.service.ICategoryService;

/**
 * 
 * Beschreibung:<br> Implementation des Interfaces für den Category-Service.
 * 
 * @author Ogün Bilge, Leif Hitzschke
 * @version 1.00, 05.11.2009
 * 
 */
public class CategoryServiceImpl implements ICategoryService,Singleton  {
	
	private final static Logger log = LoggerFactory.getLogger(CategoryServiceImpl.class);
	private IDAO<Category> categoryDao = (IDAO<Category>) DependencyManager.locate(Constants.CATEGORY_DEP_NAME);

	// Holder Idiom Pattern
	private static class Holder {
		private static final ICategoryService instance = new CategoryServiceImpl();
	}

	public static ICategoryService getInstance() {
		return Holder.instance;
	}
	
	
	@Override
	public void createCategory(Category category) {
		try {
			categoryDao.create(category);
		} catch (SQLException e) {
			log.error("error while creating record", e);
			throw new RuntimeException(e);
		}

	}
	
	@Override
	public List<Category> readAll() {
		try {
			return categoryDao.readAll();
		} catch (SQLException e) {
			log.error("error while read record", e);
			throw new RuntimeException(e);
		}

	}
	
	@Override
	public Category update(Category entity) {
		try {
			return categoryDao.update(entity);
		} catch (SQLException e) {
			log.error("error while read record", e);
			throw new RuntimeException(e);
		}

		
	}
	
	@Override
	public void delete(Category entity) {
		try {
			categoryDao.delete(entity);
		} catch (SQLException e) {
			log.error("error while delete record", e);
			throw new RuntimeException(e);
		}
	}

	@Override
	public void createDummyCategories() {
		
		try {
			List<Category> categories = categoryDao.readAll();
			
			for (Category category : categories) {
				categoryDao.delete(category);
			}
			
			Category category = new Category();
			category.setName("Sehenswürdigkeiten");
			category.setDescription("Sehenswürdigkeiten");
			categoryDao.create(category);
			
			category = new Category();
			category.setName("Hotels");
			category.setDescription("Hotels");
			categoryDao.create(category);
			
			
		} catch (SQLException e) {
			log.error("error while createDummyCategories record", e);
			throw new RuntimeException(e);
		}
		
		
	}
}
